package com.example.orderhandle.service;


import com.example.orderhandle.entity.Order;
import com.example.orderhandle.entity.OrderInfo;
import com.example.orderhandle.entity.UserDormCorrinfo;
import com.example.orderhandle.mapper.OrderMapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.*;



/**
 * @Description: 手动注入RestTemplate
 *
 * <p>启动报错：找不到RestTemplate
 *
 * @auther: zpq
 * @date: 2020/11/5 4:13 下午
 */


@Service
public class OrderServiceImp implements OrderService {

    @Autowired
    private OrderMapper orderDao;


    public void grabDorm(int roomId,int num, Order order, int Uid){

        try {
            List<Integer> userUids = new LinkedList<>();
            userUids.add(Uid);
            // 1. 更新DormRoom房间信息
            updateGrabedRoom(roomId, num);
            // 2. 写入Order表
            insertOrder(order);
            // 3. 写入OrderInfo表
            insertAllOrderInfo(order.getId(), userUids);
            // 4. 写入用户宿舍关联表
            insertUserDormCorrinfo(roomId, userUids);
        }catch (Exception e){
            System.out.println(e);
            System.out.println("数据库操作报错了");
        }
    }



    public String getCurrentTime(){
        Date date=new Date();
        SimpleDateFormat temp=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        String dateStr=temp.format(date);
        return dateStr;
    }



    @Override
    public Map<String, String> findUserInfoByUid(int uid) {
        return orderDao.findUserInfoByUid(uid);
    }



    @Override
    public List<Map<String, Object>> getDormByOrder(String buildingName, String gender, int num) {
        return orderDao.getDormByOrder(buildingName, gender, num);
    }

    @Override
    public int updateGrabedRoom(int roomId, int num) {
        return orderDao.updateGrabedRoom(roomId, num);
    }

    @Override
    public Boolean insertAllOrderInfo(int orderId, List<Integer> uid) {
        for(int i=0; i<uid.size(); i++){
            try{
                OrderInfo orderInfo = new OrderInfo();
                orderInfo.setOrderId(orderId);
                orderInfo.setUid(uid.get(i));
                int isSuccess = orderDao.insertOrderInfo(orderInfo);
                if(isSuccess == 1){
                    //成功
                    System.out.println("插入uid "+ uid + " 成功");
                }else{
                    return false;
                }
            }catch (Exception e){
                System.out.println(e);
            }
        }
        return true;
    }

    @Override
    public int findBuildingIdByBuildingName(String buildingName) {
        return orderDao.findBuildingIdByBuildingName(buildingName);
    }

    @Override
    public int insertOrder(Order order) {
        return orderDao.insertOrder(order);
    }

    @Override
    public Boolean insertUserDormCorrinfo(int roomID, List<Integer> uid) {
        for(int i=0; i<uid.size(); i++){
            try{
                UserDormCorrinfo userDormCorrinfo = new UserDormCorrinfo();
                userDormCorrinfo.setDormRoomId(roomID);
                userDormCorrinfo.setCreateTime( getCurrentTime() );
                userDormCorrinfo.setUid(uid.get(i));
                int isSuccess = orderDao.insertUserDormCorrinfo(userDormCorrinfo);
                if(isSuccess == 1){
                    //成功
                    System.out.println("插入uid "+ uid + " 成功");
                }else{
                    return false;
                }
            }catch (Exception e){
                System.out.println(e);
            }
        }
        return true;
    }
}
